﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class InvPalletBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IInvPalletDA invPalletDA = DataAccess.CreateInvPalletDA();
        private static readonly IOrderDetailDA orderDetailDA = DataAccess.CreateOrderDetailDA();
        private PubParasInfo pubParasInfo;
        private static readonly IReceiptDetailDA receiptDetailDA = DataAccess.CreateReceiptDetailDA();
        private static readonly ISkuDA skuDA = DataAccess.CreateSkuDA();

        public InvPalletBLL()
        {
            this.pubParasInfo = null;
        }

        public InvPalletBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public InvPalletBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public DataSet GetAllInvPallet()
        {
            return invPalletDA.GetAllInvPallet(this.dataBaseID[0], this.pubParasInfo);
        }

        public List<InvPalletInfo> GetInvPalletByIDAndType(string docID, string docType)
        {
            if (invPalletDA.CheckInvPalletUnique(docID, docType, this.dataBaseID[0]))
            {
                return invPalletDA.GetInvPalletByIDAndType(docID, docType, this.dataBaseID[0], this.pubParasInfo);
            }
            return invPalletDA.GetDetailByIDAndType(docID, docType, this.dataBaseID[0], this.pubParasInfo);
        }

        public string InsertInvPallet(List<InvPalletInfo> invPalletListInfo)
        {
            int i;
            string checkSku = string.Empty;
            int lineid = 0;
            string whLoginID = this.dataBaseID[0];
            for (i = 0; i < invPalletListInfo.Count; i++)
            {
                object CS00002;
                if (invPalletListInfo[i].DocType == "ASN")
                {
                    if (!receiptDetailDA.CheckReceiptDetailSkuExist(invPalletListInfo[i].DocID, invPalletListInfo[i].SkuID, whLoginID))
                    {
                        CS00002 = checkSku;
                        checkSku = string.Concat(new object[] { CS00002, invPalletListInfo[i].SkuID, "(", invPalletListInfo[i].LineID, ");" });
                    }
                }
                else if (!orderDetailDA.CheckOrderDetailSkuExist(invPalletListInfo[i].DocID, invPalletListInfo[i].SkuID, whLoginID))
                {
                    CS00002 = checkSku;
                    checkSku = string.Concat(new object[] { CS00002, invPalletListInfo[i].SkuID, "(", invPalletListInfo[i].LineID, ");" });
                }
            }
            if (!string.IsNullOrEmpty(checkSku))
            {
                PublicMethod.GetInstance().ShowAlertMessage("F4200_024", this.pubParasInfo.UserLanguage, new List<string> { invPalletListInfo[0].DocID, checkSku });
            }
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                string docID = invPalletListInfo[0].DocID;
                string docType = invPalletListInfo[0].DocType;
                if (invPalletDA.CheckInvPalletUnique(dataBase, tran, docID, docType, whLoginID))
                {
                    invPalletDA.DeleteInvPallet(dataBase, tran, docID, docType, whLoginID);
                }
                for (i = 0; i < invPalletListInfo.Count; i++)
                {
                    InvPalletInfo invPalletInfo = new InvPalletInfo();
                    lineid += 10;
                    invPalletInfo.LineID = lineid;
                    invPalletInfo.DocID = invPalletListInfo[i].DocID;
                    invPalletInfo.DocType = invPalletListInfo[i].DocType;
                    invPalletInfo.OwnerID = invPalletListInfo[i].OwnerID;
                    invPalletInfo.SkuID = invPalletListInfo[i].SkuID;
                    invPalletInfo.Case = invPalletListInfo[i].Case;
                    invPalletInfo.Pallet = invPalletListInfo[i].Pallet;
                    invPalletInfo.BillingCase = invPalletListInfo[i].BillingCase;
                    invPalletInfo.BillingPallet = invPalletListInfo[i].BillingPallet;
                    invPalletInfo.ExecDate = invPalletListInfo[i].ExecDate;
                    invPalletInfo.Remark = invPalletListInfo[i].Remark;
                    invPalletInfo.CreatedBy = invPalletListInfo[i].CreatedBy;
                    invPalletInfo.UpdatedBy = invPalletListInfo[i].UpdatedBy;
                    if (invPalletDA.InsertInvPallet(dataBase, tran, invPalletInfo, whLoginID) == 0)
                    {
                        temp = temp + invPalletInfo.LineID.ToString() + ";";
                        tran.Rollback();
                        break;
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }
    }
}

